package com.farm.project.service.impl;

import com.farm.project.domain.Tasklink;
import com.farm.core.time.TimeTool;
import org.apache.log4j.Logger;
import com.farm.project.dao.TasklinkDaoInter;
import com.farm.project.service.TasklinkServiceInter;
import com.farm.core.sql.query.DBRule;
import com.farm.core.sql.query.DBRuleList;
import com.farm.core.sql.query.DataQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import javax.annotation.Resource;
import com.farm.core.auth.domain.LoginUser;

/* *
 *功能：任务关系服务层实现类
 *详细：
 *
 *版本：v0.1
 *作者：FarmCode代码工程
 *日期：20150707114057
 *说明：
 */
@Service
public class TasklinkServiceImpl implements TasklinkServiceInter {
	@Resource
	private TasklinkDaoInter tasklinkDaoImpl;

	private static final Logger log = Logger.getLogger(TasklinkServiceImpl.class);

	@Override
	@Transactional
	public Tasklink insertTasklinkEntity(Tasklink entity, LoginUser user) {
		entity.setCuser(user.getId());
		entity.setCtime(TimeTool.getTimeDate14());
		entity.setEuser(user.getId());
		entity.setEtime(TimeTool.getTimeDate14());
		entity.setPstate("1");
		entity.setExpression("false");
		return tasklinkDaoImpl.insertEntity(entity);
	}

	@Override
	@Transactional
	public Tasklink editTasklinkEntity(Tasklink entity, LoginUser user) {
		// TODO 自动生成代码,修改后请去除本注释
		Tasklink entity2 = tasklinkDaoImpl.getEntity(entity.getId());
		// entity2.setEuser(user.getId());
		// entity2.setEusername(user.getName());
		// entity2.setEtime(TimeTool.getTimeDate14());
		entity2.setProjectid(entity.getProjectid());
		entity2.setTnodeid(entity.getTnodeid());
		entity2.setSnodeid(entity.getSnodeid());
		entity2.setPcontent(entity.getPcontent());
		entity2.setPstate(entity.getPstate());
		entity2.setCuser(entity.getCuser());
		entity2.setEuser(entity.getEuser());
		entity2.setCtime(entity.getCtime());
		entity2.setEtime(entity.getEtime());
		entity2.setId(entity.getId());
		tasklinkDaoImpl.editEntity(entity2);
		return entity2;
	}

	@Override
	@Transactional
	public void deleteTasklinkEntity(String id, LoginUser user) {
		// TODO 自动生成代码,修改后请去除本注释
		tasklinkDaoImpl.deleteEntity(tasklinkDaoImpl.getEntity(id));
	}

	@Override
	@Transactional
	public Tasklink getTasklinkEntity(String id) {
		// TODO 自动生成代码,修改后请去除本注释
		if (id == null) {
			return null;
		}
		return tasklinkDaoImpl.getEntity(id);
	}

	@Override
	@Transactional
	public DataQuery createTasklinkSimpleQuery(DataQuery query) {
		// TODO 自动生成代码,修改后请去除本注释
		DataQuery dbQuery = DataQuery.init(query, "CBTG_PROJECT_TASKLINK",
				"ID,PROJECTID,TNODEID,SNODEID,PCONTENT,PSTATE,CUSER,EUSER,CTIME,ETIME");
		return dbQuery;
	}

	@Override
	@Transactional
	public List<Tasklink> getTasklinkBySTID(String snodeid, String tnodeid) {
		List<Tasklink> links = tasklinkDaoImpl.selectEntitys(DBRuleList.getInstance()
				.add(new DBRule("SNODEID", snodeid, "=")).add(new DBRule("TNODEID", tnodeid, "=")).toList());
		return links;
	}

	@Override
	@Transactional
	public Tasklink editTasklinkExpresstion(Tasklink entity, LoginUser currentUser) {
		Tasklink entity2 = tasklinkDaoImpl.getEntity(entity.getId());
		entity2.setPcontent(entity.getPcontent());
		entity2.setExpression(entity.getExpression());
		tasklinkDaoImpl.editEntity(entity2);
		return entity2;
	}

}
